package org.diffshop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.Query;
import org.diffshop.product.dao.CategoryBrandRelationDao;
import org.diffshop.product.dao.CategoryDao;
import org.diffshop.product.dao.PmsBrandDao;
import org.diffshop.product.entity.CategoryBrandRelationEntity;
import org.diffshop.product.entity.CategoryEntity;
import org.diffshop.product.entity.PmsBrandEntity;
import org.diffshop.product.service.CategoryBrandRelationService;
import org.diffshop.product.service.PmsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private PmsBrandDao pmsBrandDao;
    @Autowired
    private PmsBrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> queryCatelogList(Long brandId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrandRelationEntity::getBrandId, brandId);
        List<CategoryBrandRelationEntity> entityList = baseMapper.selectList(queryWrapper);

        return entityList;
    }

    /**
     * 保存关联分类
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        PmsBrandEntity brand = pmsBrandDao.selectById(brandId);
        CategoryEntity category = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());

        this.save(categoryBrandRelation);
    }

    /**
     * 查询分类关联的品牌
     * @param catId
     * @return
     */
    @Override
    public List<PmsBrandEntity> queryBrandByCatId(Long catId) {
        // 这里无视关联表中的brand_name 去pms_brand表中查询更详细的信息

        // 在中间表查询brandIds
        List<Object> brandIds = baseMapper.selectObjs(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                                .select(CategoryBrandRelationEntity::getBrandId)
                                .eq(CategoryBrandRelationEntity::getCatelogId, catId));

        List<PmsBrandEntity> data = new ArrayList<>();
        if (brandIds != null && brandIds.size() > 0) {
            data = brandService.list(new LambdaQueryWrapper<PmsBrandEntity>().in(PmsBrandEntity::getBrandId, brandIds));
        }
        return data;
    }

}
